# 允许设置工具链路径
if(NOT DEFINED ARM_TOOLCHAIN_DIR)
    # set(ARM_TOOLCHAIN_DIR "C:/Users/Sherlock/.eide/tools/gcc_arm" CACHE STRING "ARM GCC工具链目录")
    set(ARM_TOOLCHAIN_DIR $ENV{ARM_GCC_ROOT})
endif()

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)

# 使用完整路径指定交叉编译器
set(CMAKE_C_COMPILER ${ARM_TOOLCHAIN_DIR}/bin/arm-none-eabi-gcc.exe)
set(CMAKE_CXX_COMPILER ${ARM_TOOLCHAIN_DIR}/bin/arm-none-eabi-g++.exe)
set(CMAKE_ASM_COMPILER ${ARM_TOOLCHAIN_DIR}/bin/arm-none-eabi-gcc.exe)
set(CMAKE_AR ${ARM_TOOLCHAIN_DIR}/bin/arm-none-eabi-ar.exe)
set(CMAKE_OBJCOPY ${ARM_TOOLCHAIN_DIR}/bin/arm-none-eabi-objcopy.exe)
set(CMAKE_OBJDUMP ${ARM_TOOLCHAIN_DIR}/bin/arm-none-eabi-objdump.exe)
set(SIZE ${ARM_TOOLCHAIN_DIR}/bin/arm-none-eabi-size.exe)


# 使用的 C 语言版本
set(CMAKE_C_STANDARD 11)
# 使用的 cpp 版本
set(CMAKE_CXX_STANDARD 17)
# 生成 compile_commands.json，可配合 clangd 实现精准的代码关联与跳转
set(CMAKE_EXPORT_COMPILE_COMMANDS True)
# 彩色日志输出
set(CMAKE_COLOR_DIAGNOSTICS True)


set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# 处理器特定的标志
set(CPU_FLAGS "-mcpu=cortex-m3 -mthumb -mfloat-abi=softfp")

# 编译标志
set(COMMON_FLAGS "${CPU_FLAGS} -ffunction-sections -fdata-sections -fno-common -fmessage-length=0")
set(CMAKE_C_FLAGS "${COMMON_FLAGS} -std=gnu11" CACHE INTERNAL "c compiler flags")
set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} -std=gnu++11" CACHE INTERNAL "cxx compiler flags")
set(CMAKE_ASM_FLAGS "${CPU_FLAGS}" CACHE INTERNAL "asm compiler flags")

# 链接标志
set(CMAKE_EXE_LINKER_FLAGS "${CPU_FLAGS} -Wl,--gc-sections -specs=nosys.specs -specs=nano.specs -lm" CACHE INTERNAL "exe link flags") 